package c.x.jy.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import c.x.jy.common.utils.SysUtils;
import c.x.jy.dao.entity.BusinessWalletEntity;
import c.x.jy.service.service.BusinessWalletService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import c.x.jy.dao.entity.BusinessEntity;
import c.x.jy.service.service.BusinessService;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 商家表
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-20 21:18:52
 */
@RestController
@RequestMapping("business/business")
public class BusinessController extends AbstractController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private BusinessWalletService businessWalletService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:business:list")
    public R list(@RequestParam Map<String, Object> params){
     	if(getUser().getBusinessId()>0) {
    		params.put("businessId", getUser().getBusinessId());
    	}
        PageUtils page = businessService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:business:info")
    public R info(@PathVariable("id") Long id){
		BusinessEntity business = businessService.getById(id);

        return R.put(business);
    }

    /**
     * 保存
     */
    @Transactional
    @RequestMapping("/save")
    @RequiresPermissions("business:business:save")
    public R save(@RequestBody BusinessEntity business){

        business.setCode(SysUtils.generateCode());
        business.setCreateDate(new Date());

		businessService.save(business);
        BusinessWalletEntity businessWalletEntity = new BusinessWalletEntity();
        businessWalletEntity.setBusinessId(business.getId());
        businessWalletService.save(businessWalletEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:business:update")
    public R update(@RequestBody BusinessEntity business){
		businessService.updateById(business);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("business:business:delete")
    public R delete(@PathVariable("id") Long id){
		businessService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

